from django.db import models
from DjangoUeditor.models import UEditorField
from ad_school.validators import validate_file_extension
from datetime import datetime
from users.models import UserProfile
# Create your models here.
class AdSchoolLabel(models.Model):
    ad_school_label = models.CharField(max_length=50, verbose_name='标签', null=False, blank=False, unique=True)
    create_time = models.DateTimeField(verbose_name='创建时间', default=datetime.now)
    update_time = models.DateTimeField(verbose_name='更新时间', default=datetime.now)

    class Meta:
        db_table = 'ad_school_label'
        verbose_name = '标签管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.ad_school_label

class AdSchool(models.Model):
    ad_school_label = models.ForeignKey(AdSchoolLabel, verbose_name='标签', null=True, blank=True, on_delete=models.SET_NULL)
    ad_school_auth = models.CharField(max_length=255, verbose_name='文章作者', null=False, blank=False)
    ad_school_title = models.CharField(max_length=255, verbose_name='标题', null=False, blank=False)
    ad_school_article = UEditorField(verbose_name='文章', null=False, blank=False, width=800, height=300, toolbars="full", imagePath='adSchoolUEditor/', filePath='adSchoolUEditor/', upload_settings={"imageMaxSize": 1204000})
    ad_school_image = models.FileField(upload_to='adSchoolImages/', verbose_name='图片', blank=False, null=False, help_text='请选择不大于50M的图片', validators=[validate_file_extension])
    ad_school_count = models.IntegerField(verbose_name='文章查看次数', null=False, blank=False, default=0)
    ad_school_laud = models.IntegerField(verbose_name='文章被赞次数', null=False, blank=False, default=0)
    ad_school_create_time = models.DateTimeField(verbose_name='文章创建时间', default=datetime.now)
    ad_school_update_time = models.DateTimeField(verbose_name='文章更新时间', default=None, null=True, blank=True)
    ad_school_browse_time = models.DateTimeField(verbose_name='最后浏览时间', default=None, null=True, blank=True)

    class Meta:
        db_table = 'ad_school_article'
        unique_together = ('ad_school_label', 'ad_school_title')
        verbose_name = '知识学院'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.ad_school_title


class AdSchoolZan(models.Model):
    article = models.ForeignKey(AdSchool, verbose_name='点赞文章', null=False, blank=False, on_delete=models.CASCADE)
    user = models.ForeignKey(UserProfile, verbose_name='点赞人', null=True, blank=True, on_delete=models.SET_NULL)
    create_time = models.DateTimeField(verbose_name='点赞时间', null=False, blank=False, default=datetime.now)

    class Meta:
        db_table = 'ad_school_zan'
        verbose_name = '点赞记录'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.article.ad_school_title

